<?php
/**
 * Created by PhpStorm.
 * User: xlosy
 * Date: 2018/3/25
 * Time: 18:22
 */

namespace app\index\controller;


use app\index\model\User;
use app\index\model\Catalog;
use app\index\model\Decade;
use app\index\model\Film;
use app\index\model\Level;
use app\index\model\Loc;
use app\index\model\Raty;
use app\index\model\Subclass;
use app\index\model\Type;
use app\index\model\Vipcode;
use think\Controller;


/**
 * Class xl
 * @package app\index\controller
 */
class xl extends Controller
{

    /**
     * 列表页面
     */
    public function index(){

        /**查询一级菜单*/
        $catalog = new Catalog();
        $return = $catalog->get_cata_log_list();
        /**传递一级菜单数据到模板*/
        $this->assign('cataLogList',$return);

        if(!is_null(input('cataLog_id'))){
            $subClass =  new Subclass();
            $subClassList =  $subClass->get_sub_class_list(input('cataLog_id'));

            /**传递数据到模板*/
            $this->assign('subClassList',$subClassList);
        }

        if(!is_null(input('subClass_id'))){
            $type =  new Type();
            $typeList =  $type->get_type_list_by_subClass_id(input('subClass_id'));

            /**传递数据到模板*/
            $this->assign('typeList',$typeList);
        }

        /**分页查询电影信息*/
        $filmModel = new Film();
        $rtn = $filmModel->get_film_list(18);

        /**传递数据到模板*/
        $this->assign('filmList',$rtn);


        /**查询筛选目录*/
        $loc = new Loc();//位置
        $rtn = $loc->get_loc_list();
        $this->assign("locList",$rtn);

        $level = new Level();//级别
        $rtn = $level->get_level_list();
        $this->assign("levelList",$rtn);

        $decade = new Decade();//级别
        $rtn = $decade->get_decade_list();
        $this->assign("decadeList",$rtn);

        /**回写目录id*/
        $this->assign("cataLog_id",input('cataLog_id'));

        return $this->view->fetch();
    }


    /**
     * 影片详情页面
     * @author skl
     * @date 2018年3月26日22:50:57
     */
    public function detail(){


        /**查询一级菜单*/
        $catalog = new Catalog();
        $return = $catalog->get_cata_log_list();
        /**传递一级菜单数据到模板*/
        $this->assign('cataLogList',$return);


        //查询当前电影
        $film =  new Film();
        $current_film = $film->get_film_by_id(input('film_id'));
        /**传递一级菜单数据到模板*/
        $this->assign('film',$current_film);
        if($current_film->isVip==1){
            $uid = session('uid');
            if($uid == null){
                $this->error("未登录");
            }else{
                if($uid->isVip!=1){
                    $this->error("你的车没油了，请加油！");
                }
            }
        }

        //查询总的评分人数
        $raty = new Raty();
        /**传递数据到模板*/
        $this->assign('totalRatys',$raty->get_raty_count());


        //根据类型查询影片
        $return = $film->get_film_by_type_id($current_film->type_id);
        /**传递数据到模板*/
        $this->assign('films',$return);

        $res = $current_film->resouces;
        $resListEd2k = array();
        $resListThunder = array();
        $resListHttp = array();
        $resListDupan = array();
        $resListFlh = array();
        $resListOther = array();
        foreach ($res as $key =>$val){

            if($val->isUse==1){
                if($val->linkType == 'ed2k'){
                    array_push($resListEd2k,$val);
                }else if($val->linkType == 'thunder'){
                    array_push($resListThunder,$val);
                }else if($val->linkType == 'http'){
                    array_push($resListHttp,$val);
                }else if($val->linkType == 'dupan'){
                    array_push($resListDupan,$val);
                }else if($val->linkType == 'Flh'){
                    array_push($resListFlh,$val);
                }else{
                    array_push($resListOther,$val);
                }
            }
        }
        /**传递数据到模板*/
        $this->assign('resListEd2k',$resListEd2k);
        $this->assign('resListThunder',$resListThunder);
        $this->assign('resListHttp',$resListHttp);
        $this->assign('resListDupan',$resListDupan);
        $this->assign('resListFlh',$resListFlh);
        $this->assign('resListOther',$resListOther);

        $this->assign('src',input("src"));
        $this->assign('j',input("j"));
        $this->assign('src1',input("src1"));
        $this->assign('j1',input("j1"));
        return $this->fetch();
    }

    /**
     * 跳转到pc直播页面
     */
    public function pc(){
        return $this->fetch();
    }


    /**
     * 跳转到pc直播页面
     */
    public function addRaty(){
        $film_id = input("film_id");
        $score = input("score");

        $raty = new Raty();
        $raty->film_id= $film_id;
        $raty->sore = $score;
        $raty->enTime = date();
        /*设置时间*/
        raty.setEnTime(DateUtil.getTime());

        $rtn = $raty->save();

        $jsonObject  = array();

        if($rtn!=0){
            /**
             *  1. 查询出所有该影片的评分
             */
            $list =  $raty->listALLByFilm_id();
            $count = 0;
            foreach ($list as $key =>$val) {
                $sc = $val->$score;
                $count = $count + $sc ;
            }

            /**
             * 2.总分除于总评分人数
             */
            $tem = $count/count($list);

            $evaluation = $tem*10/10;

            /**
             * 3.更改film的评分
             */
            $film = new Film();

            $film_rtn = $film->get_film_by_id($film_id);
            $film_rtn->evaluation= $evaluation;
            $rtn = $film_rtn->save();
            if($rtn!=0){
                $jsonObject['code'] = "1";
            }else{
                $jsonObject['code'] = "0";
            }
        }else{
            $jsonObject['code'] = "0";
        }
        return json($jsonObject);
    }


    /**
     * 进行会员加油
     */
    public function vipCodeVerification(){
        $vip_code = input("vip_code");
        $vipCode = new Vipcode();
        $vipCode = $vipCode->findByVipCode($vip_code);
        $jsonObject  = array();
        if($vipCode!=null){
            /**
                1.获取当前登录用户
             */
            $uid = session("uid");
            $user = User::get($uid->id);
            if($user!=null){

                /**获取该用户的到期时间*/
                $expireDate = $user->expireDate;
                $expireDate = strtotime($expireDate);  //int 1466028000 将英文文本日期时间解析为 Unix 时间戳：

                /**判断是否比当前时间大*/
                if($expireDate>time()){
                    $expireDate = strtotime("+1 month",$expireDate);
                }else{
                    $expireDate = strtotime("+1 month");
                }
                $user->where("id",$user->id)->update(['isVip'=>1,'expireDate'=>date("Y-m-d H:i:s",$expireDate)]);

                session("uid",User::get($uid->id));

                /**修改vipCode为不能使用，增加过期时间*/
                $vipCode->where("id",$vipCode->id)->update(['is_use'=>'0','expire_time'=>date("Y-m-d H:i:s")]);

                $jsonObject['code'] = "1";
                return json($jsonObject);
            }
        }else{
            $jsonObject['code'] = "0";
        }
        return json($jsonObject);
    }
}